<?php

namespace common\models;

use Yii;


/**
 * This is the model class for table "account_wechat".
 *
 * @property string $open_id
 * @property string $ff_id
 * @property string $create_time
 * @property string $status
 * @property string $wx_id
 * @property string $name
 * @property string $avatar
 * @property string $country
 * @property string $province
 * @property string $city
 * @property string $language
 * @property integer $sex
 * @property string $email
 */
class AccountWechat extends \yii\db\ActiveRecord
{
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{account_wechat}}';
    }

    /**
     * @return \yii\db\Connection the database connection used by this AR class.
     */
    public static function getDb()
    {
        return Yii::$app->get('db_funfind');
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['open_id', 'ff_id'], 'required'],
            [['create_time'], 'safe'],
            [['sex'], 'integer'],
            [['open_id', 'ff_id', 'wx_id', 'name', 'email'], 'string', 'max' => 32],
            [['status'], 'string', 'max' => 8],
            [['avatar'], 'string', 'max' => 128],
            [['country', 'province', 'city', 'language'], 'string', 'max' => 16],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'open_id' => '微信标识',
            'ff_id' => '系统编号',
            'create_time' => '注册时间',
            'status' => '状态',
            'wx_id' => '微信ID',
            'name' => '微信名称',
            'avatar' => '头像',
            'country' => '国家',
            'province' => '身份',
            'city' => '城市',
            'language' => '语言',
            'sex' => '性别',
            'email' => '邮箱',
        ];
    }

    public static function initRecord($params){
        $open_id  =  isset($params["open_id"]) ? $params["open_id"] : "";
        $user_open = static::find()->where(['open_id'=>$open_id])->limit(1)->one();
        if (empty($user_open)) {
            $user_open = new static();
            $user_open->open_id = $open_id;
            $user_open->status = 'ok';
            $user_open->create_time = date('Y-m-d H:i:s');

            // 初始化用户账号
            $account = [
                "nick_name" => isset($params['name']) ? $params['name'] : $open_id,
            ];
            $user_open->ff_id = AccountInfo::initRecord($account);
        }

        if ($params['wx_id']) {
            $user_open->wx_id = $params['wx_id'];
        }
        if ($params['name']) {
            $user_open->name = $params['name'];
        }
        if ($params['avatr']) {
            $user_open->avatr = $params['avatr'];
        }
        if ($params['country']) {
            $user_open->country = $params['country'];
        }
        if ($params['province']) {
            $user_open->province = $params['province'];
        }
        if ($params['city']) {
            $user_open->city = $params['city'];
        }
        if ($params['language']) {
            $user_open->language = $params['language'];
        }
        if ($params['sex']) {
            $user_open->sex = $params['sex'];
        }
        if ($params['email']) {
            $user_open->email = $params['email'];
        }
        $user_open->save();
        return $user_open->ff_id;
    }
}
